package cn.tianjindong.blog_manager.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.tianjindong.blog.common.pojo.PageView;
import cn.tianjindong.blog.mapper.BlogManagerVisitlogMapper;
import cn.tianjindong.blog.pojo.BlogManagerVisitlog;
import cn.tianjindong.blog.pojo.BlogManagerVisitlogExample;
import cn.tianjindong.blog.pojo.BlogManagerVisitlogExample.Criteria;
import cn.tianjindong.blog_manager.service.VisitLogService;

@Service
public class VisitLogServiceImpl implements VisitLogService {

	@Autowired
	private BlogManagerVisitlogMapper visitLogMapper;

	@Override
	public void addManagerVisitLog(BlogManagerVisitlog visitLog) {
		Date date = new Date();
		visitLog.setVisitTime(date);
		visitLogMapper.insert(visitLog);
	}

	@Override
	public PageView<BlogManagerVisitlog> getManagerVisitLogPageView(Integer currentPage, Integer countItem) {
		int startIndex = (currentPage - 1) * countItem;
		List<BlogManagerVisitlog> items = visitLogMapper.getLimitList(startIndex, countItem);
		PageView<BlogManagerVisitlog> pageView = new PageView<>();
		pageView.setItems(items);
		pageView.setCountItem(countItem);
		pageView.setCurrentPage(currentPage);

		// 获取数据项的条数
		int totalItem = visitLogMapper.countByExample(new BlogManagerVisitlogExample());
		pageView.setTotalItem(totalItem);
		return pageView;
	}

	@Override
	public int getManagerLoginNum(Long id) {
		BlogManagerVisitlogExample example = new BlogManagerVisitlogExample();
		Criteria criteria = example.createCriteria();
		criteria.andManagerIdEqualTo(id);
		criteria.andStatusEqualTo(1);// 只查找登录成功的数量（0表示失败，1表示成功）
		int count = visitLogMapper.countByExample(example);
		return count;
	}

	@Override
	public BlogManagerVisitlog getLastVisitLog() {
		BlogManagerVisitlog lastVisitLog = visitLogMapper.getLastVisitLog();
		return lastVisitLog;
	}
}
